/***************************************************************************
 *   Copyright (C) 2007 by ludo   *
 *   ludo42fr@free.fr   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "QVlcInput.h"
#include "QVlcInstance.h"



QVlcInput::QVlcInput ( QVlcInstance *instance )
		:QObject ( instance )
		,_vlcinstance ( instance )
		,_except ( this )
		,_input ( libvlc_playlist_get_input ( _vlcinstance->internalPointer(),_except.internalPointer() ) )
{}

QVlcInput::~QVlcInput()
{
  invalidate();
}
qint64 QVlcInput::length()
{
	_except.clear();
	if ( !isValid() )
		return 0;
	qint64 ret=libvlc_input_get_length ( _input, _except.internalPointer() );
	if ( _except.hasError() )
		return 0;
	return ret;
}


qint64 QVlcInput::time()
{
	_except.clear();
	if ( !isValid() )
		return 0;
	qint64 ret=libvlc_input_get_time ( _input, _except.internalPointer() );
	if ( _except.hasError() )
		return 0;
	return ret;
}

bool QVlcInput::setTime ( qint64 t )
{
	_except.clear();
	if ( !isValid() )
		return false;
	libvlc_input_set_time ( _input, t,_except.internalPointer() );
	if ( _except.hasError() )
		return false;
	return true;
}
double QVlcInput::position()
{
	_except.clear();
	if ( !isValid() )
		return 0.0;
	double ret=libvlc_input_get_position ( _input, _except.internalPointer() );
	if ( _except.hasError() )
		return 0.0;
	return ret;
}

bool QVlcInput::setPosition ( double p )
{
	_except.clear();
	if ( !isValid() )
		return false;
	libvlc_input_set_position ( _input, ( float ) p,_except.internalPointer() );
	if ( _except.hasError() )
		return false;
	return true;
}

double QVlcInput::rate()
{
	_except.clear();
	if ( !isValid() )
		return 0.0;
	double ret=libvlc_input_get_rate ( _input, _except.internalPointer() );
	if ( _except.hasError() )
		return 0.0;
	return ret;

}
bool QVlcInput::setRate ( double r )
{
	_except.clear();
	if ( !isValid() )
		return false;
	libvlc_input_set_rate ( _input, ( float ) r,_except.internalPointer() );
	if ( _except.hasError() )
		return false;
	return true;
}

QVlcException& QVlcInput::exception()
{
	return _except;
}


void QVlcInput::invalidate()
{
  if(_input==NULL)
    return;
  libvlc_input_free ( _input );
  _input=NULL;
}


bool QVlcInput::isValid()
{
  if(_input!=NULL)
     return true;
  //tente de recreer input automatiquement !
  _input=libvlc_playlist_get_input(_vlcinstance->internalPointer(),_except.internalPointer() );

	return _input!=NULL;
}


